﻿@using Data;
@page "/todo"

<h3>Todo (@todos.Count(x => !x.IsDone))</h3>
<ul>
    @foreach (var todo in todos)
    {
        <li>
            <input type="checkbox" @bind="@todo.IsDone" />
            <input @bind="@todo.Title" />
        </li>
    }
</ul>

<input placeholder="Something todo" @bind="newTodo" />
<button @onclick="AddTodo">Add todo</button>

@code {
    private List<TodoItem> todos = new();
    private string newTodo { get; set; }

    private void AddTodo()
    {
        if (!string.IsNullOrWhiteSpace(newTodo))
        {
            todos.Add(new TodoItem { Title = newTodo });
            newTodo = string.Empty;
        }
    }

    public override Task SetParametersAsync(ParameterView parameters)
    {
        Console.WriteLine("SetParametersAsync");
        return base.SetParametersAsync(parameters);
    }

    protected override Task OnInitializedAsync()
    {
        Console.WriteLine("OnInitialized");
        return base.OnInitializedAsync();
    }

    protected override Task OnParametersSetAsync()
    {
        Console.WriteLine("OnParametersSet");
        return base.OnParametersSetAsync();
    }

    protected override Task OnAfterRenderAsync(bool firstRender)
    {
        Console.WriteLine("OnAfterRender");
        return base.OnAfterRenderAsync(firstRender);
    }
}
